package com.enjoy.concurrent.ch6;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ThreadPoolExecutorTest {


    public static void main(String[] args) throws InterruptedException {
        System.out.println("Integer.toBinaryString(-1) = " + Integer.toBinaryString(-1));
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                1,
                2,
                1,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),
                new CustomThreadFactory("whh"),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );


        for (int i = 0; i < 100; i++) {
            long begin = System.currentTimeMillis();
            executor.execute(() -> {
                long nowBegin = System.currentTimeMillis();
                log.info("开始");

                try {
                    Thread.sleep(Integer.MAX_VALUE);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                log.info("提交到执行结束时间:{}", System.currentTimeMillis() - begin);
                log.info("开始执行到执行结束时间:{}", System.currentTimeMillis() - nowBegin);
                log.info("结束");
            });
        }


        System.out.println("executor.getPoolSize() = " + executor.getPoolSize());
        System.out.println("executor.getCorePoolSize() = " + executor.getCorePoolSize());
        System.out.println("executor.getLargestPoolSize() = " + executor.getLargestPoolSize());
        System.out.println("executor.getCompletedTaskCount() = " + executor.getCompletedTaskCount());
        System.out.println("executor.getActiveCount() = " + executor.getActiveCount());
        System.out.println("executor.getTaskCount() = " + executor.getTaskCount());
        System.out.println("executor.getQueue().size() = " + executor.getQueue().size());
        System.out.println("executor.getMaximumPoolSize() = " + executor.getMaximumPoolSize());
        Thread.sleep(1000);
    }

}
